package com.wef.system.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.ModuleTitle;
import com.wef.system.entity.domain.ModuleTitleDomain;
import com.wef.system.service.ModuleTitleService;

public class ModuleTitleServiceImpl extends BaseServiceImpl implements ModuleTitleService{

	public ModuleTitle addModuleTitle(ModuleTitle moduleTitle) {
		validateModuleTitle(moduleTitle);
		ModuleTitle newModuleTitle = (ModuleTitle)super.saveObject(moduleTitle);
		return newModuleTitle;
	}

	public ModuleTitle updateModuleTitle(ModuleTitle moduleTitle) {
		return (ModuleTitle)super.updateObject(moduleTitle);
	}

	public void removeModuleTitle(ModuleTitle moduleTitle) {

		super.deleteObject(moduleTitle);
	}

	public void removeModuleTitles(Set<ModuleTitle> moduleTitles){
		for (ModuleTitle moduleTitle : moduleTitles){
			removeModuleTitle(moduleTitle);
		}
	}

	public ModuleTitle saveOrUpdateModuleTitle(ModuleTitle moduleTitle) {
		if (moduleTitle.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addModuleTitle(moduleTitle);
		}
		if (moduleTitle.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateModuleTitle(moduleTitle);
		}
		if (moduleTitle.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.removeModuleTitle(moduleTitle);
			return ModuleTitle.NULL;
		}
		return moduleTitle;
	}

	public void saveOrUpdateModuleTitles(List<ModuleTitle> moduleTitles,String moduleId) {
		StringBuilder errorString = new StringBuilder();
		for (Iterator it = moduleTitles.iterator(); it.hasNext(); ) {
			ModuleTitle moduleTitle = (ModuleTitle)it.next();
			if (moduleTitle != null)
			{
				int rowStatus = moduleTitle.getRowStatus();
				if (moduleTitle.getModuleId() == null || moduleTitle.getModuleId() == "")
				{
					moduleTitle.setModuleId(moduleId);
				}
				try {
					moduleTitle = this.saveOrUpdateModuleTitle(moduleTitle);
				} catch (ValidateRuntimeException ex) {
					errorString.append(ex.getMessage() + "\n");
				}
				if (rowStatus == BaseObject.ROWSTATUS_DELETED)
				{
					it.remove();
				}
			}
		}
		if (errorString.length() > 0)
		{
			throw new ValidateRuntimeException(errorString.toString());
		}
	}

	public void saveOrUpdateModuleTitles(Set<ModuleTitle> moduleTitles,String moduleId) {
		StringBuilder errorString = new StringBuilder();
		for (Iterator it = moduleTitles.iterator(); it.hasNext(); ) {
			ModuleTitle moduleTitle = (ModuleTitle)it.next();
			if (moduleTitle != null)
			{
				int rowStatus = moduleTitle.getRowStatus();
				if (moduleTitle.getModuleId() == null || moduleTitle.getModuleId() == "")
				{
					moduleTitle.setModuleId(moduleId);
				}
				try {
				moduleTitle = this.saveOrUpdateModuleTitle(moduleTitle);
				} catch (ValidateRuntimeException ex) {
					errorString.append(ex.getMessage() + "\n");
				}
				if (rowStatus == BaseObject.ROWSTATUS_DELETED)
				{
					it.remove();
				}
			}
		}
		if (errorString.length() > 0)
		{
			throw new ValidateRuntimeException(errorString.toString());
		}
	}

	public void addModuleTitles(Set<ModuleTitle> moduleTitles, String moduleId) {
		StringBuilder errorString = new StringBuilder();
		try {
			deleteObjectByProperty(ModuleTitle.class, ModuleTitleDomain.moduleId, moduleId);
		} catch (ValidateRuntimeException ex) {
			errorString.append(ex.getMessage() + "\n");
		}
		
		for (Iterator<ModuleTitle> it = moduleTitles.iterator(); it.hasNext();) {
			ModuleTitle moduleTitle = (ModuleTitle) it.next();
			moduleTitle.setModuleId(moduleId);
			this.addModuleTitle(moduleTitle);
		}	
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
	}
	
	public void validateModuleTitle(ModuleTitle moduleTitle){

	}

}
